#!/usr/bin/python
# -*- coding: utf-8 -*-

import Ice, sys
import AgentTeamI, AuctioneerI
from AgentTeamI import *
from AuctioneerI import *

try:
    HOME = os.environ['ROBOTS']
except KeyError:
    print 'ROBOTS environment variable is not currently defined.'
    sys.exit(0)

SLICE = os.path.join(HOME, 'slice')
FILE = os.path.join(SLICE, 'Robots.ice')
Ice.loadSlice('--all -I/usr/share/slice', ['-I' + SLICE, FILE])

import Robots

SIGNPOSTS = 4
MAX_X = 120
MAX_Y = 120

class Server (Ice.Application):

    def run (self, argv):
        self.shutdownOnInterrupt()

        #adapter = self.communicator().createObjectAdapter('AgentServerAdapter')
        adapter = self.communicator().createObjectAdapterWithEndpoints(
            'Adapter', 'default -p 9898')
        adapter.activate()

        #props = self.communicator().getProperties()
        #team = 'AgentTeam.' + str(props.getProperty('number'))

        # TODO: remove this generation code.
        signposts = []
        for i in range (0, SIGNPOSTS):
            signposts.append(Robots.SignPost(Robots.Vec2(random.Random().randint(0, MAX_X), 
                                                          random.Random().randint(0, MAX_Y))))

        # TODO: to obtain the signposts from the camera.
        adapter.add(AgentTeamI('AgentTeam.1', signposts),
                    self.communicator().stringToIdentity('AgentTeam.1'))
        adapter.add(AuctioneerI(adapter, signposts),
                    self.communicator().stringToIdentity('Auctioneer'))

        self.communicator().waitForShutdown()

        return 0

Server().main(sys.argv, os.path.join(HOME, 'config', 'config.server'))
